package cn.com.haopy.dataes.db.service.main.impl;

import cn.com.haopy.dataes.db.entity.main.HDRole;
import cn.com.haopy.dataes.db.mapper.main.HDRoleMapper;
import cn.com.haopy.dataes.db.service.main.HDRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class HDRoleServiceImpl extends ServiceImpl<HDRoleMapper, HDRole> implements HDRoleService {

    @Override
    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();
        if (roleIds.length == 0) {
            return roles;
        }

        LambdaQueryWrapper<HDRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HDRole::getId, roleIds).eq(HDRole::getEnabled, true);

        List<HDRole> roleList = super.list(queryWrapper);

        for(HDRole role : roleList){
            roles.add(role.getName());
        }

        return roles;

    }

    @Override
    public Page<HDRole> querySelective(String name, Integer pageNo, Integer size, String sort, String order) {
        QueryWrapper<HDRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(HDRole::getName, "%" + name + "%");
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }

        Page<HDRole> page = new Page<>(pageNo, size);
        return super.page(page, queryWrapper);
    }

    @Override
    public boolean checkExistByName(String name) {
        LambdaQueryWrapper<HDRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDRole::getName, name);

        return super.count(queryWrapper) != 0;
    }

    @Override
    public List<HDRole> queryAll() {
        LambdaQueryWrapper<HDRole> queryWrapper = new LambdaQueryWrapper<>();
        return super.list(queryWrapper);
    }
}
